//
//  BusinessDetailView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/7.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BusinessDetailView.h"
#import "BusinessViewCell.h"

#import "DetailsFirstCell.h"
#import "DetailsSecondCell.h"
#import "BusinessDetailModel.h"

@interface BusinessDetailView()<UITableViewDataSource,UITableViewDelegate>
{
    MBProgressHUD *progressHUD;
}


@property(nonatomic,strong)XRCarouselView *XrCarouselView;
@property(nonatomic,strong)BusinessDetailModel *detailModel;

@end

@implementation BusinessDetailView

static NSString *const DetailsFirstIdentifier = @"DetailsFirstIdentifier";
static NSString *const DetailsSecondIdentifier = @"DetailsSecondIdentifier";

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        
        self.delegate = self;
        self.dataSource = self;

    }
    return self;
}

- (void)setBussinessId:(NSString *)bussinessId
{
    _bussinessId = bussinessId;
    
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self addSubview:progressHUD];
    
    //商家详情
    [self detailsLoadData];
    //商家产品
    //[self businessProductsData];
    //商家评论
    //[self businessReviewData];
}

#pragma mark >>>>>>>>>>网络请求
//商家详情
- (void)detailsLoadData
{
    NSString *shopid = _bussinessId;
    
    NSDictionary *param = @{@"id":[NSString stringWithFormat:@"%@", shopid]};
    
    [[NetWorkSharedTools sharedNetWorkTools] GetObjectWithURLDict:param AndMenthodName:@"api/Business/info" completeBlock:^(id object) {
        NSLog(@"%@",object);
        //code = 1 查询成功
        if ([[object objectForKey:@"code"] isEqual:@(1)]) {
            NSDictionary *tempDict = [object objectForKey:@"data"];
             _detailModel = [[BusinessDetailModel alloc] initWithDictionary:tempDict error:nil];
            NSLog(@"%@",_detailModel);
            [progressHUD hide:YES];
        }
        
        [self reloadData];
      
    }];
    
}

#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }else{
        return 3;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsSecondIdentifier];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsSecondIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor grayColor];
    }
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            DetailsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsFirstIdentifier];
            if (cell==nil) {
                
                cell = [[DetailsFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsFirstIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            //商家名
            cell.detailsName.text = _detailModel.title;
            cell.detailsNum.text = [NSString stringWithFormat:@"%d分",_detailModel.score];
            
            return cell;
        
        }else if (indexPath.row ==1){
            //地址
            cell.imageView.image = [UIImage imageNamed:@"shangjia_3"];
            cell.textLabel.text = _detailModel.dizhi;
            cell.textLabel.numberOfLines= 0;
            return cell;
            
        }else{
           
            DetailsSecondCell *cell = [DetailsSecondCell loadDetailsSecondCellView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            [cell.detailsCall setImage:[UIImage imageNamed:@"shangjia_2"] forState:UIControlStateNormal];
            //截取手机号
            NSString *phoneStr = _detailModel.dianhua;
            phoneStr = [phoneStr substringToIndex:11];
            
            [cell.detailsCall setTitle:phoneStr forState:UIControlStateNormal];
            cell.detailsCall.titleEdgeInsets = UIEdgeInsetsMake(0,5,0,0);
            cell.detailsCall.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,7);
            return cell;            
            
        }
        
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"经营范围";
            cell.textLabel.font =[UIFont systemFontOfSize:14];
            return cell;
            
        }else{
            //经营范围
            cell.textLabel.text = _detailModel.zycp;
            cell.textLabel.font =[UIFont systemFontOfSize:13];
            cell.textLabel.numberOfLines = 3;
            return cell;
        }
 
    }else {
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"商家评论";
            cell.textLabel.font =[UIFont systemFontOfSize:14];
            return cell;
        }else{
            
            return cell;
        }
 
    }

}


#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            return HEIGHT/12;
        }else{
            return HEIGHT/14;
        }
        
    }else if(indexPath.section == 1){
        
        if (indexPath.row == 0) {
            return HEIGHT/18;
        }else{
            return HEIGHT/12;
        }
    }else{
        return HEIGHT/13;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return HEIGHT/3;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return self.XrCarouselView;
    }
    return nil;
}

#pragma mark>>>>>>>>>>懒加载轮播
- (XRCarouselView *)XrCarouselView
{
    if (!_XrCarouselView) {
        NSArray *imageArr = @[
                              [UIImage imageNamed:@"HomeCycle0.jpg"],
//                              [UIImage imageNamed:@"HomeCycle1.jpg"],
//                              [UIImage imageNamed:@"HomeCycle2.jpg"]
                              ];
        _XrCarouselView = [XRCarouselView carouselViewWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/3) imageArray:imageArr];
        _XrCarouselView.time = 2;
    }
    return _XrCarouselView;
}



//商家产品
- (void)businessProductsData
{
    NSString *shopid = _bussinessId;
    
    NSDictionary *param = @{@"id":[NSString stringWithFormat:@"%@", shopid]};
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"AppShop/Getcp" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
    }];
    
}

//商家评论
- (void)businessReviewData
{
    NSString *shopid = _bussinessId;
    NSString *index = @"1";
    
    NSDictionary *param = @{@"id":[NSString stringWithFormat:@"%@", shopid], @"P":[NSString stringWithFormat:@"%@",index]};
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"AppShop/Getsjpj" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
    }];
}

@end
